package stu.aistar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import stu.aistar.dao.ICinameDao;
import stu.aistar.dao.IMovieDao;
import stu.aistar.model.pojo.Ciname;
import stu.aistar.model.pojo.Movie;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/details")
public class MovieDetailsController {
    @Autowired
    private IMovieDao iMovieDao;
    @Autowired
    private ICinameDao iCinameDao;


    @GetMapping("/detail")
    public String test(Model model,String moviename){
        List<String> actor = new ArrayList<>();

        Movie movie = iMovieDao.findMovieByName(moviename);
        actor.add(iMovieDao.findMovieByName(moviename).getActor());
        model.addAttribute("actor",actor);
        model.addAttribute("movie",movie);
        String [] actorImg1 =movie.getActorImg().split(" ");
        List<String> actorImg=new ArrayList<>();
        for (String s : actorImg1) {
            actorImg.add(s);
        }
        model.addAttribute("actorImg",actorImg);

        return "movie-details";
    }

    @GetMapping("/checkout")
    public String ciname(Model model,String moviename){
        List<String> actor = new ArrayList<>();


        Movie movie = iMovieDao.findMovieByName(moviename);
        List<Ciname> cinames = iCinameDao.findAll();


        actor.add(iMovieDao.findMovieByName(moviename).getActor());
        model.addAttribute("actor",actor);
        model.addAttribute("movie",movie);

        model.addAttribute("ciname",cinames);

        String [] actorImg1 =movie.getActorImg().split(" ");
        List<String> actorImg=new ArrayList<>();
        for (String s : actorImg1) {
            actorImg.add(s);
        }
        model.addAttribute("actorImg",actorImg);

        return "movie-ticket-plan";
    }
}